package com.easylinkin.linkappapi.openapi.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.easylinkin.linkappapi.common.exceptions.BusinessException;
import com.easylinkin.linkappapi.common.service.CommonService;
import com.easylinkin.linkappapi.common.utils.excel.ExcelConstant;
import com.easylinkin.linkappapi.common.utils.excel.ExcelTools;
import com.easylinkin.linkappapi.common.utils.io.OutputStreamUtil;
import com.easylinkin.linkappapi.openapi.mapper.SystemDockInfoMapper;
import com.easylinkin.linkappapi.openapi.dto.SystemDockInfo;
import com.easylinkin.linkappapi.openapi.service.SystemDockInfoService;
import com.easylinkin.linkappapi.security.context.LinkappUserContextProducer;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * SystemDockInfo表服务实现类
 *
 * @author CodeGenerator
 * @date 2023/05/18
 */
@Slf4j
@Service("systemDockInfoService")
public class SystemDockInfoServiceImpl extends ServiceImpl
        <SystemDockInfoMapper, SystemDockInfo> implements SystemDockInfoService {
    @Resource
    private CommonService commonService;
    @Resource
    private LinkappUserContextProducer linkappUserContextProducer;

    @Override
    public boolean saveOne(SystemDockInfo systemDockInfo) {
        commonService.setCreateAndModifyInfo(systemDockInfo);
       // systemDockInfo.setTenantId(linkappUserContextProducer.getNotNullCurrent().getTenantId());
        validParamRequired(systemDockInfo);
        validRepeat(systemDockInfo);
        validParamFormat(systemDockInfo);
        return save(systemDockInfo);
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateOne(SystemDockInfo systemDockInfo) {
        Assert.notNull(systemDockInfo.getId(), "id不能为空");
        commonService.setModifyInfo(systemDockInfo);
        //systemDockInfo.setTenantId(linkappUserContextProducer.getNotNullCurrent().getTenantId());
        validRepeat(systemDockInfo);
        validParamFormat(systemDockInfo);
        return updateById(systemDockInfo);
    }

    @Override
    public IPage<SystemDockInfo> selectPage(Page page, SystemDockInfo systemDockInfo) {
        //systemDockInfo.setTenantId(linkappUserContextProducer.getNotNullCurrent().getTenantId());
        return baseMapper.selectPage(page, systemDockInfo);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteByIds(List<Long> idList) {
        return removeByIds(idList);
    }

    @Override
    public void export(SystemDockInfo systemDockInfo, HttpServletRequest request, HttpServletResponse
            response) {

       
    }

    @Override
    public SystemDockInfo getOneById(Serializable id) {
        return baseMapper.getOneById(id);
    }

    /**
     * 校验重复
     */
    private void validRepeat(SystemDockInfo systemDockInfo) {
        /* QueryWrapper<SystemDockInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("name", systemDockInfo.getName());
        queryWrapper.eq("tenant_id", linkappUserContextProducer.getNotNullCurrent().getTenantId());
        List<SystemDockInfo> list = baseMapper.selectList(queryWrapper);
        if (list.size() == 0) {
            return;
        }
        if (list.size() > 1) {
            throw new BusinessException("名称有重复");
        }
        if (ObjectUtils.isEmpty(systemDockInfo.getId())) {
            throw new BusinessException("名称已存在");
        }
        if (!systemDockInfo.getId().equals(list.get(0).getId())) {
            throw new BusinessException("名称已存在");
        }
                    */

    }


    /**
     * 校验参数必填
     */
    private void validParamRequired(SystemDockInfo systemDockInfo) {
        //Assert.notNull(systemDockInfo, "参数为空");
        //Assert.isTrue(StringUtils.isNotBlank(systemDockInfo.getName()), "名称为空");
    }

    /**
     * 校验参数格式
     */
    private void validParamFormat(SystemDockInfo systemDockInfo) {
        //Assert.isTrue(systemDockInfo.getName() == null || systemDockInfo.getName().length() <= 50,
        //        "名称超长");
    }
}

